package com.xw.tools.view;

import cn.hutool.core.util.StrUtil;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class DDLAddColumnWin extends AbstractWin {

    private Button button = new Button("生成");

    private TextArea outputTextArea;

    private TextField tableNameField;

    private TextField columnNameField;

    private TextField columnLengthField;

    private TextField afterColumnField;

    private TextField commentField;

    private ChoiceBox cb;

    private void start() {
        Stage stage = new Stage();
        stage.setTitle("添加字段");
        //stage.initModality(Modality.WINDOW_MODAL);
        //stage.initOwner(primaryStage);
        Scene scene = new Scene(new VBox(), 800, 495);
        initView(scene);
        stage.setScene(scene);
        stage.show();
    }

    private void initView(Scene scene) {
        tableNameField = new TextField();
        tableNameField.setPromptText("输入表名");
        columnNameField = new TextField();
        columnNameField.setPromptText("数据列名");
        cb = new ChoiceBox(FXCollections.observableArrayList(
                "int", "bigint", "varchar", "char", "datetime")
        );
        cb.setValue("varchar");
        columnLengthField = new TextField();
        columnLengthField.setPromptText("长度");
        afterColumnField = new TextField();
        afterColumnField.setPromptText("after列名");
        commentField = new TextField();
        commentField.setPromptText("注释");

        outputTextArea = new TextArea();
        outputTextArea.setFont(Font.font(16));
        outputTextArea.setEditable(false);
        button.setOnAction((ActionEvent t) -> {
            String tableName = tableNameField.getText();
            String columnName = columnNameField.getText();
            if (StrUtil.isBlank(tableName) || StrUtil.isBlank(columnName)) {
                return;
            }
            StringBuilder sb = new StringBuilder("ALTER TABLE ");
            sb.append(tableName);
            sb.append(" ADD ");
            sb.append(columnName);
            sb.append(" ");
            sb.append(cb.getValue());
            if (StrUtil.isNotBlank(columnLengthField.getText())) {
                sb.append("(");
                sb.append(columnLengthField.getText());
                sb.append(")");
            }
            if (StrUtil.isNotBlank(afterColumnField.getText())) {
                sb.append(" AFTER ");
                sb.append(afterColumnField.getText());
            }
            if (StrUtil.isNotBlank(commentField.getText())) {
                sb.append(" COMMENT ");
                sb.append(commentField.getText());
            }
            sb.append(";");
            outputTextArea.setText(sb.toString());
        });
        VBox box = (VBox) scene.getRoot();
        box.getChildren().add(tableNameField);
        box.getChildren().add(columnNameField);
        box.getChildren().add(cb);
        box.getChildren().add(columnLengthField);
        box.getChildren().add(afterColumnField);
        box.getChildren().add(commentField);
        box.getChildren().add(button);
        box.getChildren().add(outputTextArea);
        VBox.setVgrow(outputTextArea,  Priority.ALWAYS);
    }

    @Override
    public void show() {
        start();
    }
}
